import { createSlice, PayloadAction } from '@reduxjs/toolkit'
export type UserStateType = {
    username: string
    nickname: string
}
const INIT_STATE: UserStateType = {
    username: '',
    nickname: '',
}
export const userSlice = createSlice({
    name: 'user',
    initialState: INIT_STATE,
    reducers: {
        // 登录是重新设置新的username和nickname
        loginReducer: (state: UserStateType, action: PayloadAction<UserStateType>) => {
            // 设置username和nickname到store中
            return action.payload
        },
        // 退出时，返回初始化数据就可以 [ 退出就是清空当前的username和nickname ]
        logoutReducer: () => INIT_STATE,
    },
})

export const { loginReducer, logoutReducer } = userSlice.actions
export default userSlice.reducer
